# coding=utf-8
import re

NUMBER_DICT = {
    ('一', '壹', '１', '1'): 1,
    ('二', '贰', '２', '2'): 2,
    ('三', '叁', '３', '3'): 3,
    ('四', '肆', '４', '4'): 4,
    ('五', '伍', '５', '5'): 5,
    ('六', '陆', '６', '6'): 6,
    ('七', '柒', '７', '7'): 7,
    ('八', '捌', '８', '8'): 8,
    ('九', '玖', '９', '9'): 9,
    ('零', '０', '0'): 0,
    ('十', '拾'): 10,
    ('百', '佰'): 100,
    ('千', '仟'): 1000,
    ('万', '萬'): 10000,
    ('亿', '億'): 100000000,
    ('.',): '.'
}

NUMBER_MATCH_RES = [
    re.compile('([一二三四五六七八九〇十百千万亿]*?)(元|百万|万元|亿元|万|亿|角|分)'),
    re.compile('([壹贰叁肆伍陆柒捌玖零拾佰仟萬億]*?)(元|百万|万元|亿元|万|亿|角|分)'),
    re.compile('([１２３４５６７８９０]*?)(元|百万|万元|亿元|万|亿|角|分)'),
    re.compile('(\d*[\d,，]*\.?\d*)(元|百万|万元|亿元|万|亿|角|分)'),
    re.compile('(0\.\d+)(元|百万|万元|亿元|万|亿|角|分)')
]
cn_re = re.compile(r'^[〇一二三四五六七八九零十百千万亿壹贰叁肆伍陆柒捌玖拾佰仟萬億]+$')
str_number_re = re.compile(r'^[１２３４５６７８９０\d\\.]+$')
simple_str_number_unit = re.compile(r'^\d+[十拾百佰千仟万萬亿億]$')


def convert_to_number(data):
    """转换字符串为数字，支持中文大小写数字，小数等"""
    if is_cn_number(data):
        amount = cn_to_number(data)
    elif is_str_number(data):
        amount = str_number_to_number(data)
    elif is_number_unit(data):
        amount = number_unit_to_number(data)
    else:
        # 其他，类似于混合型，单位后还有数字24万5百
        cn = convert_to_cn(data)
        amount = cn_to_number(cn)
    return amount


def format_number(raw):
    for k, v in NUMBER_DICT.items():
        if raw in k:
            return v
    return 0


def cn_to_number(cn: str) -> int:
    result = 0
    unit = 1
    for i, w in enumerate(reversed(cn)):
        num = format_number(w)
        if isinstance(num, str):
            continue

        if num >= 10 and i == len(cn) - 1:
            if num > unit:
                unit = num
                result += num
            else:
                unit *= num
                result += unit
        elif num >= 10:
            if num > unit:
                unit = num
            else:
                unit *= num
        else:
            result += unit * num
    return result


def convert_to_cn(string):
    result = ''
    for w in string:
        for k, v in NUMBER_DICT.items():
            if w == v:
                result += k[0]
    return result


def str_number_to_number(str_number):
    result = ''
    for i, w in enumerate(str_number):
        num = format_number(w)
        result += str(num)
    if not result:
        return 0

    if '.' in result:
        return float(result)
    else:
        return int(result)


def number_unit_to_number(data):
    unit = data[len(data) - 1]
    unit = format_number(unit)
    number = data[:len(data) - 1]
    if not number.isdigit():
        return 0

    if '.' in number:
        return float(number) * unit
    else:
        return int(number) * unit


def is_cn_number(string):
    return bool(cn_re.findall(string))


def is_str_number(string):
    return bool(str_number_re.findall(string))


def is_number_unit(string):
    """12万"""
    return bool(simple_str_number_unit.findall(string))
